//
//  SMMainViewController.swift
//  lishiminProduct
//
//  Created by p2p on 16/10/8.
//  Copyright © 2016年 p2p. All rights reserved.
//

import UIKit

class SMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupChildControllers()
    }

}
// MARK: - 设置界面
extension SMMainViewController {
    //设置所有子控制器
    fileprivate func setupChildControllers() {
        let array = [
            ["clsName":"SMOptionalViewController","title":"自选","imageName":"home"],
            ["clsName":"SMMarkerViewController","title":"行情","imageName":"message_center"],
            ["clsName":"SMBusinessViewController","title":"交易","imageName":"profile"],
            ["clsName":"SMQuanziViewController","title":"圈子","imageName":"discover"],
            ["clsName":"SMAccountViewController","title":"账户","imageName":"profile"],
        ]
//        let array = [
//            ["clsName":"SMOptionalViewController","imageName":"choose"],
//            ["clsName":"SMMarkerViewController","imageName":"market"],
//            ["clsName":"SMBusinessViewController","imageName":"information"],
//            ["clsName":"SMQuanziViewController","imageName":"information"],
//            ["clsName":"SMAccountViewController","imageName":"account"],
//            ]
        
        var arrayM = [UIViewController]()
        
        for dict in array  {
            
            arrayM.append(controller(dict: dict))
        }
        viewControllers = arrayM
        
    }
    
    /// 使用字典创建一个子控制器
    ///
    /// - parameter dic: 信息字典[clsName,title,imageName]
    ///
    /// - returns: 子控制器
    private func controller(dict: [String: String]) -> UIViewController {
        
        // 1.取得字典内容
        guard let clsName = dict["clsName"],
                let title = dict["title"],
                let imageName = dict["imageName"],
                let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as? UIViewController.Type
            else {
                
               return UIViewController()
        }
        // 2.创建视图控制器
        
        let vc = cls.init()
        vc.title = title
        let nav = SMNavigationController(rootViewController: vc)
        
        //3.设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        //4.设置tabbar的标题字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .highlighted)
//        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 24)], for: .normal)
        
        return nav
        
    }
}
